<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/1
 * Time: 16:46
 */

namespace app\admin\controller;

/**
 * 作者信息
 */
class Author extends Permissions
{
    /**
     * 作者信息编辑页
     */
    public function updateView(){
        $model = new \app\admin\model\Author();
        $data = $model->getAuthorInfo();
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 作者信息保存
     */
    public function save(){
        $param = request()->param();
        $model = new \app\admin\model\Author();
        $author_info = $model->getAuthorInfo();
        if(empty($author_info)){
            //新增
            $data['name'] = $param['name']??'';
            $data['desc'] = $param['desc']??'';
            $data['avatar'] = $param['avatar']??'';
            $data['job'] = $param['job']??'';
            $data['wechat'] = $param['wechat']??'';
            $data['email'] = $param['email']??'';
            $res = $model->insert($data);
        }else{
            //更新
            $data['name'] = $param['name']??$author_info['name'];
            $data['desc'] = $param['desc']??$author_info['desc'];
            $data['avatar'] = $param['avatar']??$author_info['avatar'];
            $data['job'] = $param['job']??$author_info['job'];
            $data['wechat'] = $param['wechat']??$author_info['wechat'];
            $data['email'] = $param['email']??$author_info['email'];
            $res = $model->where(['id'=>$author_info['id']])->update($data);
        }
        if($res){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }
}